/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007 Intel Corporation. All Rights Reserved.
//
//
*/

#ifndef __UMC_AVS_LIST_H
#define __UMC_AVS_LIST_H

namespace UMC
{

template <class T>
class AVSListElement : public T
{
public:
    // Default constructor
    AVSListElement(void)
    {
        m_pNext = 0;
    }

    inline
    AVSListElement<T> *GetNext(void)
    {
        return m_pNext;
    }

    inline
    void SetNext(AVSListElement<T> *pNext)
    {
        m_pNext = pNext;
    }

protected:
    AVSListElement<T> *m_pNext;                                 // (AVSListElement<T> *) pointer to the next item in a list
};

// this class is a wrapper of simple lists' operations

template <class T>
class AVSList
{
public:
    // Default constructor
    AVSList(void)
    {
        m_pHead = 0;
    }

    void Reset(void)
    {
        m_pHead = 0;
    }

    inline
    AVSListElement<T> *GetHead(void)
    {
        return m_pHead;
    }

    AVSListElement<T> *ExtractHead(void)
    {
        AVSListElement<T> *pTemp = m_pHead;

        if (pTemp)
        {
            // move the list head
            m_pHead = pTemp->GetNext();

            // remove link to the list
            pTemp->SetNext(0);
        }

        return pTemp;
    }

    void AddToHead(AVSListElement<T> &element)
    {
        element.SetNext(m_pHead);
        m_pHead = &element;
    }

    void AddToTail(AVSList<T> &list)
    {
        if (m_pHead)
        {
            AVSListElement<T> *pTemp = m_pHead;

            // find the last element in the list
            while (pTemp->GetNext())
                pTemp = pTemp->GetNext();

            // concatenate the lists
            pTemp->SetNext(list.GetHead());
        }
        else
            m_pHead = list.GetHead();

        list.Reset();
    }

    void AddToTail(AVSListElement<T> &element)
    {
        AVSList<T> tmp;

        tmp.AddToHead(element);

        AddToTail(tmp);
    }

protected:
    AVSListElement<T> *m_pHead;                                 // (AVSListElement<T> * *) pointer to the first item in the list
};

} // namespace UMC

#endif // __UMC_AVS_LIST_H
